package com.banciyuan.bcywebview.biz.search;

import android.content.Context;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.api.SearchApi;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.SearchContent;
import de.greenrobot.daoexample.model.TagDetail;
import de.greenrobot.daoexample.model.Team;
import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by squall on 2016/4/13.
 */
public class SearchHelper {

    private RequestQueue queue;
    private Context context;
    private ISearchhelper iSearchhelper;

    public SearchHelper(Context context, ISearchhelper iSearchhelper) {
        this.context = context;
        queue = VolleyQueue.getRquest(context);
        this.iSearchhelper = iSearchhelper;
    }

    private String getUrl(String type) {
        if (type.equals("user")) {
            return HttpUtils.BASE_URL + SearchApi.getSearchUser();
        } else if (type.equals("work")) {
            return HttpUtils.BASE_URL + SearchApi.getSearchWorks();
        } else if (type.equals("tag")) {
            return HttpUtils.BASE_URL + SearchApi.getSearchTags();
        } else if (type.equals("cos")) {
            return HttpUtils.BASE_URL + SearchApi.getSearchCos();
        } else if (type.equals("illust")) {
            return HttpUtils.BASE_URL + SearchApi.getSearchIllust();
        } else if (type.equals("write")) {
            return HttpUtils.BASE_URL + SearchApi.getSearchNovel();
        } else if (type.equals("daily")) {
            return HttpUtils.BASE_URL + SearchApi.getSearchDaily();
        } else if (type.equals("group")) {
            return HttpUtils.BASE_URL + SearchApi.getSearchGroup();
        } else if (type.equals("post")) {
            return HttpUtils.BASE_URL + SearchApi.getSearchPost();
        } else if (type.equals("goods")) {
            return HttpUtils.BASE_URL + SearchApi.getSearchGoods();
        } else {
            return HttpUtils.BASE_URL + SearchApi.getSearchContent();
        }
    }

    public void getSearch(String type, int page, String query) {
        getSearch(type, page, query, "");
    }

    public void getSearch(String type, int page, String query, String limte) {
        List<BcyPair> BcyPairs = new ArrayList<BcyPair>();
        BcyPairs.add(new BcyPair(HttpUtils.INDEX_P, String.valueOf(page)));
        BcyPairs.add(new BcyPair(HttpUtils.SEARCH_QUERY, query));
        BcyPairs.add(new BcyPair(HttpUtils.USER_TOKEN, UserDataHelper.getInstance(context).getToken()));
        if (!TextUtils.isEmpty(limte)) {
            BcyPairs.add(new BcyPair(HttpUtils.LIMIT, limte));
        }
        Map<String, String> map = HttpUtils.getData(BcyPairs);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, context)) {
                    iSearchhelper.succ("user", s);
                } else {
                    iSearchhelper.fail("user");
                }
            }
        };
        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                iSearchhelper.fail("user");
            }
        };
        String url = getUrl(type);
        OnErrorProxy errorProxy = new OnErrorProxy(onError, onSucc, url, context, map);
        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, errorProxy);
        queue.add(requestParam);
    }

    public static List<SearchContent> popIllegal(List<SearchContent> templist, int type) {
        List<SearchContent> results = new ArrayList<SearchContent>();
        for (SearchContent searchContent : templist) {
            if (TimelineFactory.whichType(searchContent) == type) {
                if (searchContent.getIs_error() != 1) {
                    results.add(searchContent);
                }
            }
        }
        return results;
    }

    public static List<Timeline> popIllegal(List<Timeline> templist, int[] types) {
        List<Timeline> results = new ArrayList<Timeline>();
        for (Timeline timeline : templist) {
            for (int i = 0; i < types.length; i++) {
                if (TimelineFactory.whichType(timeline) == types[i]) {
                    results.add(timeline);
                    break;
                }
            }
        }
        return results;
    }

    public static List<Team> popIllegal(List<Team> templist) {
        List<Team> results = new ArrayList<>();
        for (Team team : templist) {
            if (!TextUtils.isEmpty(team.getWork())) {
                TagDetail tagDetail = new TagDetail();
                tagDetail.setTag_name(team.getWork());
                tagDetail.setWork(team.getWork());
                team.getTags().add(0, tagDetail);
            }
            if (team.getOtype().equals("group") && team.getOtype_data().equals("group")) {
                results.add(team);
            }
        }
        return results;
    }


}
